//
//  LGGroupManageController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/24.
//  分组管理

#import "LGGroupManageController.h"
//分组成员
#import "LGGroupMemberController.h"

#import "LGGroupManageCell.h"
//编辑名称
#import "LGInputAlertView.h"
@interface LGGroupManageController ()<UITableViewDelegate,UITableViewDataSource,LGGroupManageDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *addButton;
/**   */
@property (nonatomic , strong)NSArray *dataArray;

@end

@implementation LGGroupManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"分组管理";
    self.navigationView.rightBtn.frame = CGRectMake(Screen_W-viewPix(80), statusBarHeight, viewPix(70), 44);
    self.navigationView.rightBtn.titleLabel.font = LGFontWeight(14, UIFontWeightMedium);
    [self.navigationView.rightBtn setTitle:@"新增分组" forState:UIControlStateNormal];
    [self.navigationView.rightBtn setTitleColor:[UIColor colorWithString:@"#4C87FD"] forState:UIControlStateNormal];
    [self.navigationView.rightBtn addTarget:self action:@selector(addGroupAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight)];
    self.emptyView.backgroundColor = [UIColor clearColor];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.group/listAll" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        self.tableView.hidden = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.dataArray = returnData;
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无分组" offset:0];
            self.tableView.hidden = YES;
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        self.tableView.hidden = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:0];
            self.tableView.hidden = YES;
        }
    }];
}

//添加分组
-(void)addGroupAction{
    LGInputAlertView *nameView = [[LGInputAlertView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    nameView.placeHolder = @"请输入分组名称";
    nameView.title = @"添加分组";
    __weak typeof(self) weakSelf = self;
    nameView.groupNameFinished = ^(NSString * _Nonnull content) {
        [RequestUtil withPOST:@"/doctor/patient.group/save" parameters:@{@"title":content} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [weakSelf requestData];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            
        }];
    };
    [self.view.window addSubview:nameView];
}

//编辑分组
-(void)editGroup:(NSDictionary *)dataDic{
    LGInputAlertView *nameView = [[LGInputAlertView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    nameView.placeHolder = @"请输入分组名称";
    nameView.title = @"编辑分组名称";
    __weak typeof(self) weakSelf = self;
    __block NSString *groupid = LGNSString(dataDic[@"id"]);
    nameView.groupNameFinished = ^(NSString * _Nonnull content) {
        [RequestUtil withPOST:@"/doctor/patient.group/save" parameters:@{@"title":content,@"id":groupid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [weakSelf requestData];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            
        }];
    };
    [self.view.window addSubview:nameView];
}

//删除分组
-(void)deleteGroup:(NSDictionary *)dataDic{
    __block NSString *groupid = LGNSString(dataDic[@"id"]);
    [LGAlertTool alertWithTitle:@"删除分组" content:@"确定删除该分组？" sureAction:^{
        __weak typeof(self) weakSelf = self;
        [RequestUtil withPOST:@"/doctor/patient.group/delete" parameters:@{@"ids":groupid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [weakSelf requestData];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            
        }];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGGroupManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGGroupMemberController *controller = [[LGGroupMemberController alloc]init];
    controller.titleStr = LGNSString(tempDic[@"title"]);
    controller.groupid = LGNSString(tempDic[@"id"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight, Screen_W, Screen_H-topBarHeight) style:UITableViewStylePlain];
        [_tableView registerClass:[LGGroupManageCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(45);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.hidden = YES;
        LGCornerHeaderView *headerView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
        headerView.backgroundColor = [UIColor clearColor];
        headerView.topMargin = viewPix(10);
        headerView.cornerRidus = viewPix(5);
        _tableView.tableHeaderView = headerView;
        
        LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
        footerView.backgroundColor = [UIColor clearColor];
        footerView.bottomMargin = viewPix(10);
        footerView.cornerRidus = viewPix(5);
        _tableView.tableFooterView = footerView;
    }
    return _tableView;
}

-(UIButton *)addButton{
    if(!_addButton){
        _addButton = [UIButton buttonWithTitle:@"新增分组" titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:@"#429BFF" imageName:@""];
        _addButton.frame = CGRectMake(0, 0, 60, 44);
        [_addButton addTarget:self action:@selector(addGroupAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
